package TAIC.util;

import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.Vector;

public class MergeResults {

	/**
	 * @param args
	 * Example: ~ 65 1 20
	 */
	public static void main(String[] args) throws Exception {
		if ( args.length != 3 ) {
			System.out.println ( "please input the experiemt ID, the begin number and the end number") ;
			return ;
		}
		( new MergeResults () ).work ( args ) ; 
	}	
	
	void work ( String [] args) throws Exception {
		Vector < double [] > result = new Vector < double [] > () ;
		int i1 = Integer.valueOf( args [ 1 ]) ;
		int i2 = Integer.valueOf( args [ 2 ]) ;
		String prefix = args [ 0 ]  + "_"  ;
		int count = 0 ;
		for ( int i = i1 ; i <= i2 ; i ++ ) {
			Scanner scanner;
			try{
				scanner = new Scanner ( new File ( prefix + i + ".txt") ) ;
			}
			catch ( Exception e ){
				continue ;
			}
			int index = 0 ;
			while ( scanner.hasNextDouble() ) {
				double [] p ; 
				if ( index >= result.size() ) p = new double [ i2 - i1 + 1 ]  ; else p = result.get ( index ) ;
				p [ count ] = scanner.nextDouble() ;
				if ( index >= result.size() ) result.add( p ) ; else result.set(index, p) ; 
				index ++ ; 
			}
			scanner.close() ;
			count ++ ;
		}
		
		PrintStream fout = new PrintStream ( new File ( "New_exp.txt" ) ) ;
		for ( int i = 0 ; i < result.size() ; i ++ ) {
			for ( int j = 0 ; j < count ; j ++ ) fout.printf( "%.5f\t", result.get(i)[ j ] ) ;
			fout.println () ; 
		}
		fout.close() ;
	}

}
